<?php
include '../newlib.php';
include '../lib/AppScope.pre.php';

function editPage($program, $position) {
	global $tmpConnection, $app, $PRODUCTNAME, $theUser, $loggedInUser;
	include "header.php";
	//echo "<h1>" . $program . "(" . $position . ")</h1>";
	$stm = $tmpConnection->prepare('select dur from content where id=:id');
	$stm->execute(array(':id' => $position));
	$rs = $stm->fetch(PDO::FETCH_ASSOC);
	$dur = $rs['dur'];
	$sql = "select * from vwcontent where id=" . $position;
	$stm = $tmpConnection->prepare($sql);
	$stm->execute();
	$rs = $stm->fetch(PDO::FETCH_ASSOC);
	$titel = $rs['titel'];
	$inhoud = str_replace("<br />", "\n", $rs['inhoud']);
	$background = $rs['background'];
	$special = $rs['special'];
    echo <<<EOD
		<h2>Pagina $position aanpassen</h2>
		<form class="form-horizontal" role="form" name="frmEdit" method="post" action="savepage.php?program=$program">
		  <input type="hidden" name="page" value="$position" />
          <div class="row">
            <div class="col-md-6">
          <div>
            <label class="sr-only" for="titel">Titel van de pagina</label>
            <div>
		      <input id="titel" class="form-control" type="text" size="30" name="titel" value="$titel" aria-describedby="help-titel">
		    </div>
 		    <span id="help-titel" class="help-block">als u de tekst tussen haakjes () zet, wordt deze niet weergegeven</span>
		  </div>
              <div>
                <label class="sr-only" for="inhoud">Inhoud van de pagina</label>
		        <textarea id="inhoud" class="form-control" name="inhoud" rows="11" cols="60"  aria-describedby="help-inhoud">$inhoud</textarea>
		      </div>
              <span id="help-inhoud" class="help-block">Om een tabel te maken kunt u het '~'-symbool gebruiken als TAB Voor een euro-symbool kunt u [euro] intikken.</span>
		    </div>
		    <div class="col-md-6">
		      <div class="form-group">
                <div class="col-md-6 col-md-offset-6">
		          <img class="img-responsive img-rounded" name="preview" src="" />
		        </div>
		      </div>
		      <div class="form-group">
		        <label for="background" class="control-label col-md-4">Achtergrondplaatje:</label>
		        <div class="col-md-8">
  		          <select id="background" class="form-control" name="background" onchange="imgPreview(this.value);" >
EOD
    ;
    $schemaid = $rs['schemaid'];
	$script = '';
	$plaatje = "";
	$stm = $tmpConnection->prepare('select * from images where listed or id=:id order by image');
	$stm->execute(array(':id' => $background));
	while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
		$imageId = $row['id'];
		$imageName = $row['image'];
		if ($imageId == $background) {
			$plaatje = $imageName;
			echo "	  	          <option selected value=\"$imageId\">$imageName</option>";
		} else {
			echo "		          <option value=\"$imageId\">$imageName</option>";
		}
		$script .= "if (num==".$imageId.") document.preview.src='../images/thumbs/".str_replace("'", "'", $imageName)."';\n";

	}

	echo <<<EOD
		          </select>
		        </div>
		      </div>
		      <div class="form-group">
		<!--SPECIALS -->
		<label for="special" class="control-label col-md-4">Speciale pagina's:</label>
		<div class="col-md-8">
		  <select id="special" class="form-control" name="special" onchange="checkTijd()">
EOD;

	if ($special == 0) {
		echo '<option selected value="0">Normale pagina</option>\n';
	} else {
		echo '<option value="0">Normale pagina</option>\n';
	}
	$stm = $tmpConnection->prepare('select * from special order by titel');
	$stm->execute();
	while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
		$specialId = $row['id'];
		$specialTitel = $row['titel'];
		if ($specialId == $special) {
			echo "<option selected value=\"$specialId\">$specialTitel</option>\n";
		} else {
			echo "<option value=\"$specialId\">$specialTitel</option>\n";
		}
	}
	echo <<<EOD
		    </select>
		  </div>
		</div>

		<!-- TIJD -->
		<div class="form-group">
		  <label for="tijd" class="control-label col-md-4">Weergavetijd:</label>
		  <div class="col-md-8">
	  	    <div class="input-group">
	  	      <input id="tijd" class="form-control" type="text" size="10" name="dur" value="$dur" aria-describedby="help-tijd"/>
		      <span class="input-group-addon">seconden</span>
		    </div>
            <span id="help-tijd" class="help-block">Laat leeg voor de standaard tijd,<br>standaard tekst: 18 sec en enkel plaatjes: 9 sec</span>
		  </div>
        </div>

		<!-- UITZENDSCHEMA -->
		<div class='row'>
EOD
	;

	if ($schemaid != null) {
		$now = time();
		echo "<strong class='col-md-4'>Deze pagina volgt een uitzendschema</strong>";
		echo "<div class='btn-group col-md-8' role='toolbar'><a href=\"javascript:show_window2('schema.php?id=$schemaid&now=$now)', 'schema', 540, 380);\" class='btn btn-default'>Aanpassen...</a><a href=\"delschema.php?id=$schemaid&position=$position&program=$program\" class='btn btn-default'>Verwijderen</a></div>";
	} else {
		echo "<strong class='col-md-4'>Uitzendschema</strong>";
		echo "<div class='btn-group col-md-8' role='toolbar'><a href=\"javascript:show_window2('newschema.php?id=$position', 'schema', 540, 380);\" class='btn btn-default'>Aanmaken...</a></div>";
	}

	echo <<<EOD
	    </div>
		</div></div>

		<input type="button" value="Voorbeeld" onclick="pagePreview();">
		<input type="button" value="Bewaar" onclick="bewaar();">
		</form>

		<a href="javascript:history.go(-1);"><font color="Black">Terug...</font></a>

		<script language="JavaScript">
		document.preview.src = "../images/thumbs/$plaatje";
		checkTijd();

		function checkTijd() {
			if (document.frmEdit.special.value == "0") {
				document.frmEdit.dur.disabled = false;
				document.all["dur0"].style.visibility = "visible";
			} else {
				document.frmEdit.dur.value = "";
				document.frmEdit.dur.disabled = true;
				document.all["dur0"].style.visibility = "hidden";
			}
		}

		function show_window2(whichpage, namepage, winwidth, winheight) {
			window.open (whichpage, namepage, 'scrollbars=yes,resizable=1,status=no,toolbar=no,location=no,menubar=no,width=' + winwidth + ',height=' + winheight + '')
		}

		function imgPreview(num) {
$script;
		}

		function pagePreview() {
			imgnr = document.frmEdit.background.value;
			special = document.frmEdit.special.value;
			titel = document.frmEdit.titel.value;
			inhoud1 = document.frmEdit.inhoud.value;
			inhoud2 = "";
			regels = 1;
			for (t=0;t<=inhoud1.length;t++) {
				c = inhoud1.substring(t,t+1);
				if (c == '\\n') {
					inhoud2 = inhoud2 + "<br>";
					regels = regels + 1;
				} else {
					if (c=="&") {
						inhoud2 = inhoud2 + "%26";
					} else {
						if (c=="%") {
							inhoud2 = inhoud2 + "%25";
						} else {
							inhoud2 = inhoud2 + c;
						}
					}
				}
			}
			if (regels <= 15) {
				window.open("kabelkrant.php?command=preview&thispage=$position&titel=" + titel + "&inhoud=" + inhoud2 + "&imgnr=" + imgnr + "&special=" + special, "popUp", "fullscreen=yes, scrollbars=no, width=1365, height=768");
			} else {
				alert("U kunt niet meer dan 15 regels gebruiken!");
			}
		}

		function bewaar() {
			inhoud1 = document.frmEdit.inhoud.value;
			inhoud2 = "";
			regels = 1;
			for (t=0;t<=inhoud1.length;t++) {
				c = inhoud1.substring(t,t+1);
				if (c == '\\n') {
					inhoud2 = inhoud2 + "<br>";
					regels = regels + 1;
				} else {
					inhoud2 = inhoud2 + c;
				}
			}
			if (regels <= 15) {
				document.frmEdit.submit();
			} else {
				alert("U kunt niet meer dan 15 regels gebruiken!");
			}
		}

		</script>
EOD
	;

	include "footer.php";
}
?>
